<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/7
 * Time: 10:23
 */

namespace app\admin\controller;


use think\Request;
use think\Db;

class Qrcode extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where = [];
            //名称
            if(isset($params['in_name']) && $params['in_name'] != "") {
                $where['ucw.in_name'] = ['like', "%" . $params['in_name'] . "%"];
            }
            //规格类型
            if(isset($params['spec_type_id']) && $params['spec_type_id'] != '') {
                $where['ucw.spec_type_id'] = $params['spec_type_id'];
            }
            //房屋类型
            if(isset($params['house_type_id']) && $params['house_type_id'] != '') {
                $where['ucw.house_type_id'] = $params['house_type_id'];
            }
            //仓库
            if(isset($params['house_id']) && $params['house_id'] != '') {
                $where['ucw.warehouse_id'] = $params['house_id'];
            }
            //未删除
            $where['ucw.is_del'] = 1;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;// 获取总条数;
            $count = Db::table('up_code_warehouse')->count();
            $lists = Db::table('up_code_warehouse')
                ->alias('ucw')
                ->join('up_house_type uht', 'uht.house_type_id = ucw.house_type_id', 'left')
                ->join('up_spec_type ust', 'ust.spec_type_id = ucw.spec_type_id', 'left')
                ->join('up_warehouse uw', 'uw.house_id = ucw.warehouse_id', 'left')
                ->field(['code_id', 'in_num', 'house_type', 'spec_type', 'warehouse_name',
                    'in_name', "concat('".http_type()."',in_img)"=>'in_img', 'in_abstract'])
                ->page($Nowpage, $limits)
                ->where($where)
                ->order('code_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        //规格类型
        $specType = Db::table('up_spec_type')
            ->field('spec_type_id, spec_type')
            ->select();
        //房屋类型
        $houseType = Db::table('up_house_type')
            ->field('house_type_id, house_type')
            ->select();
        //仓库
        $warehouse = Db::table('up_warehouse')
            ->field('house_id, warehouse_name')
            ->select();
        $this->assign([
            'specType' => $specType,
            'houseType' => $houseType,
            'warehouse' => $warehouse
        ]);
        return $this->fetch();
    }

    /**
     * 添加二维码入库记录
     * @return mixed|\think\response\Json
     */
    public function warehouseQrCodeAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $data = [
                'in_num' => $params['in_num'],//编号
                'in_name' => $params['in_name'],//名称
                'in_img' => $params['in_img'],//缩略图
                'house_type_id' => $params['house_type_id'],//房屋类型ID
                'spec_type_id' => $params['spec_type_id'],//规格类型ID
                'warehouse_id' => $params['warehouse_id'],//仓库
                'in_address' => $params['in_address'],//地址
                'in_abstract' => $params['in_abstract'],//摘要
            ];
            $res = Db::table('up_code_warehouse')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功', 'data' => '']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败', 'data' => '']);
            }
        }
        //规格类型
        $specType = Db::table('up_spec_type')
            ->field('spec_type_id, spec_type')
            ->select();
        //房屋类型
        $houseType = Db::table('up_house_type')
            ->field('house_type_id, house_type')
            ->select();
        //仓库
        $warehouse = Db::table('up_warehouse')
            ->field('house_id, warehouse_name')
            ->select();
        $this->assign([
            'specType' => $specType,
            'houseType' => $houseType,
            'warehouse' => $warehouse
        ]);
        return $this->fetch('qrcode/warehouseqrcodeadd');
    }

    /**
     * 编辑二维码入库记录
     * @return mixed|\think\response\Json
     */
    public function warehouseQrCodeEdit(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $update = [
                'in_num' => $params['in_num'],//编号
                'in_name' => $params['in_name'],//名称
                'in_img' => $params['in_img'],//缩略图
                'house_type_id' => $params['house_type_id'],//房屋类型
                'spec_type_id' => $params['spec_type_id'],//规格类型ID
                'warehouse_id' => $params['warehouse_id'],//仓库
                'in_address' => $params['in_address'],//地址
                'in_abstract' => $params['in_abstract'],//摘要
            ];
            $res = Db::table('up_code_warehouse')->where('code_id', $params['code_id'])->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功', 'data' => '']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败', 'data' => '']);
            }
        }
        $code_id = input('id');
        $info = Db::table('up_code_warehouse')->where('code_id', $code_id)->find();
        //规格类型
        $specType = Db::table('up_spec_type')
            ->field('spec_type_id, spec_type')
            ->select();
        //房屋类型
        $houseType = Db::table('up_house_type')
            ->field('house_type_id, house_type')
            ->select();
        //仓库
        $warehouse = Db::table('up_warehouse')
            ->field('house_id, warehouse_name')
            ->select();
        $this->assign([
            'specType' => $specType,
            'houseType' => $houseType,
            'warehouse' => $warehouse,
            'info' => $info
        ]);
        return $this->fetch('qrcode/warehouseqrcodeedit');
    }

    /**
     * 删除二维码入库记录
     * @return \think\response\Json
     */
    public function warehouseQrCodeDel(){
        $code_id = input('param.id');
        $res = Db::table('up_code_warehouse')->where('code_id', $code_id)->setField('is_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功', 'data' => '']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败', 'data' => '']);
        }
    }

    /**
     * 生成编号
     * @return \think\response\Json
     */
    public function createNum(){
        $spec_id = input('param.spec_id');
        //前缀
        $spec_code = Db::table('up_spec_type')->where('spec_type_id', $spec_id)->value('spec_code');
        //入库记录二维码表最大ID
        $maxId = Db::table('up_code_warehouse')->max('code_id');
        $newQrcodeId = $maxId + 1;
        $leng = strlen($newQrcodeId);
        $zero = '';
        //补0
        if($leng < 5){
            for($i = $leng; $i < 5; $i++){
                $zero .= '0';
            }
        }
        //入库表最大ID
        $inMaxId = Db::table('up_in_warehouse')->max('in_id');
        $newInQrcodeId = $inMaxId + 1;
        $leng1 = strlen($newInQrcodeId);
        $zero1 = '';
        //补0
        if($leng1 < 5){
            for($i = $leng1; $i < 5; $i++){
                $zero1 .= '0';
            }
        }
        $in_num = $spec_code . '-' . $zero .$newQrcodeId. $zero1 . $newInQrcodeId;
        return json(['status' => 200, 'data' => $in_num]);
    }

    /**
     * 编辑二维码入库记录时，修改编号
     * @return \think\response\Json
     */
    public function editNum(){
        $spec_id = input('spec_id');
        $old_num = input('old_num');
        //前缀
        $spec_code = Db::table('up_spec_type')->where('spec_type_id', $spec_id)->value('spec_code');
        $old_num = explode('-', $old_num);
        //更换前缀
        $old_num[0] = $spec_code;
        $in_num = implode('-', $old_num);
        return json(['status' => 200, 'data' => $in_num]);
    }

    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function outExcel(){
        $params = input('param.');
        if($params['ids'] == "" && $params['in_name'] == "" && $params['spec_type_id'] == "" && $params['house_type_id'] == "" && $params['house_id'] ==""){
            $data = Db::table('up_code_warehouse')
                ->alias('ucw')
                ->join('up_house_type uht', 'uht.house_type_id = ucw.house_type_id', 'left')
                ->join('up_spec_type ust', 'ust.spec_type_id = ucw.spec_type_id', 'left')
                ->join('up_warehouse uw', 'uw.house_id = ucw.warehouse_id', 'left')
                ->field(['code_id', 'in_num', 'house_type', 'spec_type', 'warehouse_name',
                    'in_name', "concat('".http_type()."',in_img)"=>'in_img', 'in_abstract', 'in_address'])
                ->order('code_id desc')
                ->select();
        }
        $where = [];
        if($params['ids'] != ""){
            $ids = trim($params['ids'],',');
            $ids = explode(',',$ids);
            $where['code_id'] = ['IN', $ids];
//            $data = Db::name('in_warehouse')->where('in_id', 'IN', $ids)->select();
        }else{
            //名称
            if(isset($params['in_name']) && $params['in_name'] != "") {
                $where['ucw.in_name'] = ['like', "%" . $params['in_name'] . "%"];
            }
            //规格类型
            if(isset($params['spec_type_id']) && $params['spec_type_id'] != '') {
                $where['ucw.spec_type_id'] = $params['spec_type_id'];
            }
            //房屋类型
            if(isset($params['house_type_id']) && $params['house_type_id'] != '') {
                $where['ucw.house_type_id'] = $params['house_type_id'];
            }
            //仓库
            if(isset($params['house_id']) && $params['house_id'] != '') {
                $where['ucw.warehouse_id'] = $params['house_id'];
            }
        }
        $data = Db::table('up_code_warehouse')
            ->alias('ucw')
            ->join('up_house_type uht', 'uht.house_type_id = ucw.house_type_id', 'left')
            ->join('up_spec_type ust', 'ust.spec_type_id = ucw.spec_type_id', 'left')
            ->join('up_warehouse uw', 'uw.house_id = ucw.warehouse_id', 'left')
            ->field(['code_id', 'in_num', 'house_type', 'spec_type', 'warehouse_name',
                'in_name', "concat('".http_type()."',in_img)"=>'in_img', 'in_abstract', 'in_address'])
            ->where($where)
            ->order('code_id desc')
            ->select();

        $cellname = [
            ['code_id','ID',15,'LEFT'],
            ['in_name','名称',15,'LEFT'],
            ['in_num','单据编号',15,'LEFT'],
            ['in_img','缩略图',20,'LEFT'],
            ['house_type','房屋类型',15,'LEFT'],
            ['spec_type','规格类型',15,'LEFT'],
            ['warehouse_name','仓库',15,'LEFT'],
            ['in_abstract','摘要',30,'LEFT'],
            ['in_address','地址',30,'LEFT']
        ];
        $res = exportExcel('库存列表','Qrcode-buildings',$cellname,$data);
        return json($res);
    }
}